package com.example.androidpractice.reposity;

import android.util.Log;

import com.example.androidpractice.model.Task;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TaskResposity {
    public List<Task> getTaskByCatalog(String catalog) {
        final List<Task> tasks = new ArrayList<>();
        OkHttpClient okHttpClient = new OkHttpClient();
        //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
        Request request = new Request.Builder()
                .url("https://www.studyinghome.com/mock/5e5e2ca76b63043d83e13a54/example/tasks")
                .get()
                .build();
        okHttpClient.newCall(request)
                .enqueue(new Callback() {
                             @Override
                             public void onFailure(Call call, IOException e) {
                                 Log.w("test",e);
                             }

                             @Override
                             public void onResponse(Call call, Response response) throws IOException {
                                 Gson gson = new Gson();
                                 tasks.addAll(gson.fromJson(response.body().string(), new TypeToken<ArrayList<Task>>() {
                                 }.getType()));

                             }
                         }

                );
        return tasks;
    }
}
